package com.scalar.chiptrack.finance.creditNote.actionCommands;

import com.scalar.ScalarException;
import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.finance.creditNote.dao.CreditNoteDAO;
import com.scalar.chiptrack.finance.creditNote.forms.CreditNoteForm;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.sales.LineItem;
import com.scalar.chiptrack.utils.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Sujan
 * Date: Apr 27, 2007
 * Time: 5:20:06 PM
 */
public class CreditNoteModifyCommand extends ActionCommand 
{
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    {
        HttpSession session  = request.getSession();

        User user            = (User) session.getAttribute ( SessionKeys.USER_KEY );

        CreditNoteForm creditNoteForm = ( CreditNoteForm ) form;

        HashMap errors = ( HashMap )request.getAttribute( SessionKeys.ERROR_MESSAGES );
        HashMap info = ( HashMap )request.getAttribute( SessionKeys.INFO_MESSAGES );
        try
        {
            if( creditNoteForm.getCreditNoteId() > 0 )
            {
                CreditNoteDAO.loadCreditNote(null, creditNoteForm);

                formatLineItemQuantities( creditNoteForm );
            }
        }
        catch ( ScalarException e )
        {
            e.printStackTrace();
            errors.put ( "Could not Load Credit Note", null );
        }
            return mapping.findForward ( "creditNoteModify" );
    }

    private void formatLineItemQuantities( CreditNoteForm creditNoteForm )
    {
        ArrayList allLineItems          = creditNoteForm.getLineItems();
        ArrayList cancelledLineItems    = creditNoteForm.getCancelledLineItems();
        LineItem lineItem               = null;
        int size                        = 0;

        if( "Credit".equalsIgnoreCase( creditNoteForm.getNoteType() ) )
        {
            size = ( allLineItems != null ) ? allLineItems.size() : 0;
            for ( int i = 0; i < size; i++ )
            {
                lineItem = ( LineItem ) allLineItems.get( i );
                lineItem.setAdjustmentQty( - lineItem.getAdjustmentQty() );
                lineItem.setDiscountAmount( - lineItem.getDiscountAmount() );
            }

            lineItem    = null;
            size        = ( cancelledLineItems != null ) ? cancelledLineItems.size() : 0;
            for ( int i = 0; i < size; i++ )
            {
                lineItem = ( LineItem ) cancelledLineItems.get( i );
                lineItem.setAdjustmentQty( - lineItem.getAdjustmentQty() );
                lineItem.setDiscountAmount( - lineItem.getDiscountAmount() );
            }
            creditNoteForm.setTotalAdjustmentAmount( - creditNoteForm.getTotalAdjustmentAmount() );
        }
    }
}
